import numpy as np
import pandas as pd
import time


def process_weather(dir_path, save_path, month_length):
    map_list = {'多云': 'cloudy', '阴': 'overcast', '阴转多云': 'overcasttocloudy', '阴转小雨': 'overcasttosmallrain',
                '小雨': 'smallrain', '阴转晴': 'overcasttosunny', '多云转晴': 'cloudytosunny', '晴': 'sunny',
                '多云转阴': 'cloudytoovercast',
                '多云转小雨': 'cloudytosmallrain', '中雨转小雨': 'midraintosmallrain', '雨': 'rain'}

    data = pd.read_csv(dir_path + 'weather.csv')
    data = data.drop(data.tail(1).index)
    data['climate'] = data['climate'].map(map_list)
    data['time'] = pd.to_datetime(data['time'])
    data['month_day'] = data['time'].apply(lambda x: x.day)
    data['hour'] = data['time'].apply(lambda x: x.hour)
    month_day = pd.DataFrame([[i + 1, 0] for i in range(month_length)], columns=['month_day', 'key'])
    hour = pd.DataFrame([[i, 0] for i in range(24)], columns=['hour', 'key'])
    temp = pd.merge(month_day, hour, on='key', how='outer')
    temp.drop(['key'], axis=1, inplace=True)
    data = pd.merge(temp, data, on=['month_day', 'hour'], how='outer')
    data = data.fillna(method='ffill')
    data_weather = pd.get_dummies(data, prefix='climate', columns=['climate'])
    data_weather.drop('wind_direction', axis=1, inplace=True)
    data_weather.drop(1, axis=0, inplace=True)

    data_weather['climate_sunny'] = data_weather['climate_overcasttosunny']+data_weather['climate_cloudytosunny']+\
                                    data_weather['climate_sunny']
    data_weather['climate_overcast'] = data_weather['climate_overcast'] + data_weather['climate_overcasttocloudy'] +\
                                       data_weather['climate_overcasttosmallrain'] +\
                                       data_weather['climate_overcasttosunny']
    data_weather['climate_rainy'] = data_weather['climate_overcasttosmallrain'] + data_weather['climate_smallrain'] + \
                                    data_weather['climate_cloudytosmallrain'] + data_weather['climate_rain']
    data_weather['climate_cloudy'] = data_weather['climate_cloudy'] + data_weather['climate_overcasttocloudy'] + \
                                     data_weather['climate_cloudytosunny'] + data_weather['climate_cloudytoovercast'] +\
                                     data_weather['climate_cloudytosmallrain']
    data_weather['climate_sunny'] = data_weather['climate_sunny'].map(lambda x: 1 if x > 0 else 0)
    data_weather['climate_overcast'] = data_weather['climate_overcast'].map(lambda x: 1 if x > 0 else 0)
    data_weather['climate_rainy'] = data_weather['climate_rainy'].map(lambda x: 1 if x > 0 else 0)
    data_weather['climate_cloudy'] = data_weather['climate_cloudy'].map(lambda x: 1 if x > 0 else 0)
    drop_target = ['climate_overcasttocloudy', 'climate_overcasttosmallrain', 'climate_smallrain',
                   'climate_overcasttosunny', 'climate_cloudytosunny','climate_cloudytoovercast',
                   'climate_rain', 'climate_cloudytosmallrain', 'climate_midraintosmallrain']
    data_weather = data_weather.drop(drop_target, axis=1)
    data_weather.to_csv(save_path + 'weather_data.csv',index=False)